$(window).load(function() {
	var _tableListCache = [];
	var _AlltableCache = [];
	var _moduleCache;
	
	$("#divTableDraggable").droppable({
		drop : function (event, ui) {
			moduleAddTable(ui.draggable.text());
		}
	});
	
	$("#divDelTableDraggable").droppable({
		drop : function (event, ui) {
			if (_moduleCache != undefined) {
				var sel = $("#tableList");
				var tindex = $.inArray(ui.draggable.text(), _tableListCache);
				if (tindex >= 0) {
					var param = JSON.stringify({'moduleId':_moduleCache,'tableName': ui.draggable.text()});
					$.ajax({
						type : 'POST',
						contentType : 'application/json',
						url : 'delRelation.do',
						data : param,
						dataType : 'json',
						success : function(data) {
							_tableListCache.splice(tindex, 1);
							setTimeout(function() { ui.draggable.remove(); }, 1);
						},
						error : function () {
							alert("删除失败");
						}
					});
				}
			}
		}
	});
	
	$.ajax({
		type : 'POST',
		contentType : 'application/json',
		url : 'getModuleList.do',
		data : '',
		dataType : 'json',
		success : function(data) {
			var sel = $("#moduleSelect");
			$.each(data, function(i, n) {
				sel.append('<li class="ui-widget-content ui-corner-all" value="' + n.moduleId + '">'
						+ n.moduleName + '</li>');
			});
			$("#moduleSelect li").click(function() {
				$("#moduleSelect li").removeClass("ui-selected");
				$(this).addClass("ui-selected");
				var param = $(this).attr("value").toString();
				$.ajax({
					type : 'POST',
					contentType : 'application/json',
					url : 'getTableList.do',
					data : param,
					dataType : 'json',
					success : function(data) {
						var sel = $("#tableList");
						sel.empty();
						_tableListCache = [];
						$.each(data, function(i, n) {
							sel.append('<li class="ui-widget-content ui-corner-all" value="'
									+ n.tableName + '">'
									+ n.tableName + '</li>');
							_tableListCache.push(n.tableName);
						});
						_moduleCache = param;
						sel.find("li").draggable({ scroll: false, revert: true ,helper: 'clone'});
					}
				// error : failed
				});
			});
		},
		error : function() {
			alert("尼玛，失败了");
		}
	});
	
	$.ajax({
		type : 'POST',
		contentType : 'application/json',
		url : 'getAllTableList.do',
		data : '',
		dataType : 'json',
		success : function(data) {
			var sel = $("#allTableList");
			_AlltableCache = [];
			$.each(data, function(i, n) {
				sel.append('<li class="ui-widget-content ui-corner-all" value="' + n.tableName + '">'
						+ n.tableName + '</li>');
				_AlltableCache.push(n.tableName);
			});
			sel.find("li").draggable({ helper: 'clone', scroll: false });
			$( "#tableNameAutocomplete" ).autocomplete({
				source: _AlltableCache
			});
		}
	});
	
	$("#chooseTable").button({icons: {primary:'ui-icon-check'}});
	
	$("#chooseTable").click(function() {
		if ($("#tableNameAutocomplete").val() != undefined 
				&& $("#tableNameAutocomplete").val().length > 0
				&& $.inArray($("#tableNameAutocomplete").val(), _AlltableCache) > 0) {
			moduleAddTable($("#tableNameAutocomplete").val());
		}
	});
	
	var moduleAddTable = function(tableName) {
		if (_moduleCache != undefined) {
			var sel = $("#tableList");
			if ($.inArray(tableName, _tableListCache) < 0) {
				var param = JSON.stringify({'moduleId':_moduleCache,'tableName': tableName});
				$.ajax({
					type : 'POST',
					contentType : 'application/json',
					url : 'addRelation.do',
					data : param,
					dataType : 'json',
					success : function(data) {
						_tableListCache.push(tableName);
						var li = $('<li class="ui-widget-content ui-corner-all" value="' + tableName + '">' + tableName + '</li>');
						sel.append(li);
						li.draggable({ revert: true, scroll: false, helper: 'clone' });
					},
					error : function () {
						alert("添加失败");
					}
				});
			}
		}
	};
});